package com.gzw.dms.KJ;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import com.google.gson.reflect.TypeToken;
import com.gzw.dms.MyApplication;
import com.gzw.dms.R;
import com.gzw.dms.adapter.SelectDepartAdapter;
import com.gzw.dms.base.BaseActivity;
import com.gzw.dms.bean.DepartInfo;
import com.gzw.dms.bean.UserInfo;
import com.gzw.dms.utils.HttpUtils;
import com.gzw.dms.utils.JsonUtil;
import com.gzw.dms.utils.T;
import com.gzw.dms.utils.UrlUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SelectDepartActivity extends BaseActivity {
	private ProgressDialog pd;
	private Context mContext;
	private ListView lv_depart;
	private Button btn_query,btn_sure;
	private EditText et_depart;
	private List<DepartInfo> ls;
	private SelectDepartAdapter arr_adapter;
	private String tag = "SelectDepartActivity---->";
	private List<String> ls_map;
	private List<String> ls_name;
	private String type = "0";
	private Intent mIntent;
	private LinearLayout layout_sure;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	@Override
	protected void setContentView() {
		setContentView(R.layout.activity_select_depart);

	}

	@Override
	protected void initData() {
		mContext = this;
		mIntent = getIntent();
		ls_map = new ArrayList<>();
		ls_name = new ArrayList<>();
		type=(String)mIntent.getExtras().get("type");
		if (type.equals("1")){
			layout_sure.setVisibility(View.GONE);
		}
	}

	public void initViews() {
		lv_depart = (ListView) findViewById(R.id.lv_depart);
		btn_query = (Button) findViewById(R.id.btn_query);
		btn_sure = (Button) findViewById(R.id.btn_sure);
		et_depart = (EditText) findViewById(R.id.et_depart);
		layout_sure = (LinearLayout) findViewById(R.id.layout_sure);

	}

	@Override
	protected void initEvents() {
		// TODO Auto-generated method stub
		btn_query.setOnClickListener(new MyOnClick());
		btn_sure.setOnClickListener(new MyOnClick());
		lv_depart.setOnItemClickListener(new myOnItemClickListener());
	}

	class MyOnClick implements OnClickListener {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
				case R.id.btn_sure:
					if (ls != null) {

					for (int i = 0; i < ls.size(); i++) {
						if (arr_adapter.getIsSelected().get(i)) {
							ls_map.add(ls.get(i).getDepartment_id());
							ls_name.add(ls.get(i).getDepartment_name());

						}
					}
					if (ls_map.size() == 0) {
						T.showShort(mContext, "请先选择部门!");
					} else {
						Intent intent = new Intent();
						intent.putStringArrayListExtra("id", (ArrayList<String>) ls_map);
						intent.putStringArrayListExtra("name", (ArrayList<String>) ls_name);
						setResult(0, intent);
						finish();
					}}
					break;

				default:
					getDepartment();
					break;
			}

		}
	}

	class myOnItemClickListener implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			Intent intent = new Intent();
			intent.putExtra("id", ls.get(position).getDepartment_id()
					.toString());
			intent.putExtra("name", ls.get(position).getDepartment_name()
					.toString());
			setResult(0, intent);
			finish();
		}
	}

	public void getDepartment() {
		if (isNull(et_depart)) {
			ShowOrderTask showOrderTask = new ShowOrderTask();
			showOrderTask.execute("");
		}
		ShowOrderTask showOrderTask = new ShowOrderTask();
		showOrderTask.execute(et_depart.getText().toString().trim());
	}

	public class ShowOrderTask extends AsyncTask<String, Void, String> {
		//onPreExecute方法用于在执行后台任务前做一些UI操作
		@Override
		protected void onPreExecute() {

		}

		protected String doInBackground(String... params) {

			Map<String, Object> map = new HashMap<>();
			UserInfo userInfo = MyApplication.getUserInfo();
			map.put("userInfo", userInfo);
			map.put("departmentName", params[0]);
			String json = JsonUtil.toJson(map);
			return HttpUtils.doPost(UrlUtils.getInstance().getJavaUrl("appGetDepartmentInfo"), "json=" + json);
		}

		@Override
		protected void onPostExecute(final String result) {

			if (isNull(result)) {
				T.showShort(mContext, "连接服务器异常");
			} else {
				if (result.indexOf("errorMsg") > 0) {
					String errorMsg = JsonUtil.getMsg(result, "errorMsg");
					T.showShort(mContext, errorMsg);
				} else {
					String successMsg = JsonUtil.getMsg(result, "successMsg");
					ls = JsonUtil.getPersons(successMsg, new TypeToken<List<DepartInfo>>() {
					}.getType());
					if (ls.size()==0){
						T.showShort(mContext, "没有查到相关信息！");
						arr_adapter = new SelectDepartAdapter(mContext, ls,type);
						lv_depart.setAdapter(arr_adapter);
					}else {
					arr_adapter = new SelectDepartAdapter(mContext, ls,type);
					lv_depart.setAdapter(arr_adapter);
				}
				}
			}
		}
	}

}
